﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Data;
using InfoTech.Core.Data.SqlClient;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class HOGIADINHService : BaseService
    {
        public static bool Check_Insert_HOGIADINH(HOGIADINH hogiadinh)
        {
            bool result = true;
            var dataContext = CreateDataContext();
            result &= dataContext.GetTable<HOGIADINH>().FirstOrDefault(c => c.MACHUHO == hogiadinh.MACHUHO) == null;
            return result;
        }

        public static bool Check_Update_HOGIADINH(HOGIADINH hogiadinh)
        {
            return true;
        }

        public static bool Check_Delete_HOGIADINH(string[] strings)
        {
            bool result = true;
            var dataContext = CreateDataContext();
            result &= dataContext.GetTable<HOGIADINH_GIAODAN>().Where(c => strings.Contains(c.MAHOGIADINH + "") && (c.DACHUYENDI.HasValue == false || c.DACHUYENDI == false)).Count() <= 1;
            return result;
        }

        public static void Delete(HOGIADINH hogiadinh)
        {
            HOGIADINH_GIAODANService.Delete(hogiadinh);
            HOGIADINH_DIACHIService.Delete(hogiadinh);
        }

        public static IEnumerable<SelectListItem> List(string magiaoho)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Selected = true, Text = "---{Chọn Hộ Gia Đình}---" });
            DataItem dataItem = new DataItem();
            dataItem["MAGIAOHO"] = magiaoho;
            list.AddRange(
                InfoTech.Core.Data.Receiver.UnTypeObject.SelectMany(dataItem, "sp_HoGiaDinh_GiaoHo").Select(
                    c =>
                    new SelectListItem() { Selected = false, Text = c["TENCHUHO"] + "", Value = c["MAHOGIADINH"] + "" }));
            return list;
        }
    }
}
